package com.ccys.cms.service.corporateAndInsurance.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.corporateAndInsurance.OnboardingScreensService;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.corporateAndInsurance.OnboardingScreensDetailDto;
import com.ccys.db.dto.corporateAndInsurance.OnboardingScreensDto;
import com.ccys.db.entity.TblOnboardingScreens;
import com.ccys.db.entity.TblOnboardingScreensDetail;
import com.ccys.db.mapper.TblOnboardingScreensDetailMapper;
import com.ccys.db.mapper.TblOnboardingScreensMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.corporateAndInsurance.OnboardingScreensListSelectVo;
import com.ccys.db.vo.corporateAndInsurance.OnboardingScreensPageQueryVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 演示接口实现类
 * @author: ZhangJingCan
 * @create: 2023-12-20 15:12
 **/

@Service
public class OnboardingScreensServiceImpl extends BaseServiceImpl<TblOnboardingScreens> implements OnboardingScreensService {

    /**
     * 演示
     */
    @Resource
    private TblOnboardingScreensMapper tblOnboardingScreensMapper;

    /**
     * 主要用于修改状态时保存操作记录
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    /**
     * 演示详情数据
     */
    @Resource
    private TblOnboardingScreensDetailMapper tblOnboardingScreensDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveOnboardingScreens(OnboardingScreensDto dto) {
        //校验字段
        Kit.allParamValid(dto, "name", "display", "cta", "routeUsers");
        //校验完字段直接开始新增或修改 - 这里不需要校验是否会重复数据
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteOnboardingScreens(OnboardingScreensDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblOnboardingScreens tblOnboardingScreens = new TblOnboardingScreens();
        tblOnboardingScreens.setId(dto.getId());
        tblOnboardingScreens.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblOnboardingScreensMapper.updateByPrimaryKeySelective(tblOnboardingScreens) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateOnboardingScreensStatus(OnboardingScreensDto dto) {
        //校验
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getStatus())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据修改
        TblOnboardingScreens tblOnboardingScreens = new TblOnboardingScreens();
        tblOnboardingScreens.setId(dto.getId());
        tblOnboardingScreens.setStatus(dto.getStatus());
        if (tblOnboardingScreensMapper.updateByPrimaryKeySelective(tblOnboardingScreens) > 0) {
            //保存状态修改日志
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
            return ResponseResult.success("Modified successfully!");
        }
        return ResponseResult.error("Modification failed!");
    }

    @Override
    public ResponseResult<PageInfo<OnboardingScreensPageQueryVo>, Object> findPageOnboardingScreens(OnboardingScreensDto dto) {
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        //开启分页
        PageDto.startPage();
        List<OnboardingScreensPageQueryVo> list = tblOnboardingScreensMapper.findPageOnboardingScreens(dto);
        PageInfo<OnboardingScreensPageQueryVo> info = new PageInfo<>(list);
        return ResponseResult.success(info);
    }

    @Override
    public ResponseResult<PageInfo<OnboardingScreensPageQueryVo>, Object> findListOnboardingScreensDetail(OnboardingScreensDetailDto dto) {
        //校验是否传入主数据ID
        if (ObjectUtil.isEmpty(dto.getOnboardingScreensId())) {
            return ResponseResult.error("Parameter abnormality!");
        }
        Example example = new Example(TblOnboardingScreensDetail.class);
        example.createCriteria().andEqualTo("onboardingScreensId", dto.getOnboardingScreensId());
        List<TblOnboardingScreensDetail> list = tblOnboardingScreensDetailMapper.selectByExample(example);
        return ResponseResult.success(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveOnboardingScreensDetail(OnboardingScreensDetailDto dto) {
        //校验
        Kit.allParamValid(dto, "androidImage", "iosImage");
        if (ObjectUtil.isEmpty(dto.getId())) {
            if (tblOnboardingScreensDetailMapper.insertSelective(dto) > 0) {
                return ResponseResult.success("Added successfully");
            }
            return ResponseResult.error("Add failed");
        } else {
            if (tblOnboardingScreensDetailMapper.updateByPrimaryKeySelective(dto) > 0) {
                return ResponseResult.success("Modified successfully");
            }
            return ResponseResult.error("Modification failed");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateOnboardingScreensDetailOrder(OnboardingScreensDetailDto dto) {
        if (ObjectUtil.isEmpty(dto.getId()) || ObjectUtil.isEmpty(dto.getDispOrder())) {
            return ResponseResult.error("Parameter abnormality");
        }
        //准备数据对象修改
        TblOnboardingScreensDetail detail = new TblOnboardingScreensDetail();
        detail.setId(detail.getId());
        detail.setDispOrder(detail.getDispOrder());
        if (tblOnboardingScreensDetailMapper.updateByPrimaryKeySelective(detail) > 0) {
            return ResponseResult.success("Modified successfully");
        }
        return ResponseResult.error("Modification failed");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteOnboardingScreensDetail(OnboardingScreensDetailDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblOnboardingScreensDetail detail = new TblOnboardingScreensDetail();
        detail.setId(dto.getId());
        detail.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (tblOnboardingScreensDetailMapper.updateByPrimaryKeySelective(detail) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult<List<OnboardingScreensListSelectVo>, Object> listSelect() {
        List<OnboardingScreensListSelectVo> list = tblOnboardingScreensMapper.listSelect();
        return ResponseResult.success(list);
    }
}
